package com.proxy.proxy2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 为类实现动态代理
 */
public class ClassProxy implements InvocationHandler {

    /**
     * 被代理的类
     */
    public Object obj;

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("ClassProxy  start  ....");
        System.out.println("method modifiers  ...." + method.getModifiers());
        Object o = method.invoke(obj, args);
        System.out.println("ClassProxy  end  ....");
        return o;
    }


    public Object getProxy(Object proxyObject) {
        this.obj = proxyObject;
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
    }
}
